package com.service;

import com.domain.User;
import com.domain.result.ResultData;
import com.mall.AESUtil;
import com.mapper.primary.UserMapper;
import com.service.seinterface.UserService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public List<User> login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }


    @Override
    public ResultData addUser(User user) {
        user.setPassword(AESUtil.encrypt(user.getPassword(),AESUtil.PASSWORD));
        int count=userMapper.insertUser(user);
        ResultData resultData=new ResultData();
        if(count>0){
            resultData.setMessage("添加成功");
        }else{
            resultData.setResult(1);
            resultData.setMessage("添加失败");
        }
        return resultData;
    }

    @Override
    public ResultData selectAll() {
        ResultData resultData=new ResultData();
        resultData.setData(userMapper.selectAll());
        return resultData;
    }

    @Override
    public ResultData deleteUserById(String userid) {
        ResultData resultData=new ResultData();
        if(userid.equals("")||userid==null){
            resultData.setResult(1);
            resultData.setMessage("删除失败");
        }else{
        int count=userMapper.deleteByPrimaryKey(Integer.valueOf(userid));
        if(count>0){
            resultData.setMessage("删除成功");
        }else{
            resultData.setResult(1);
            resultData.setMessage("删除失败");
        }
        }
        return resultData;
    }

    @Override
    public ResultData updateUserById(User user) {
        int count=userMapper.updateByPrimaryKey(user);
        ResultData resultData=new ResultData();
            if(count>0){
                resultData.setMessage("修改成功");
            }else{
                resultData.setResult(1);
                resultData.setMessage("修改失败");
            }
        return resultData;
    }

}
